import csv

from flask import Flask, render_template, json

app = Flask(__name__)


# 编写读取数据函数read，便于应用
def read(res):
    data = []
    with open(res, encoding='utf8') as f:
        f_csv = csv.reader(f)
        headers = next(f_csv)
        # print(headers)
        for row in f_csv:
            data.append({headers[0]: row[0], headers[1]: row[1], headers[2]: row[2]})
            # print(row)
    return data


@app.route('/')
def hello_world():
    return render_template('index.html')


@app.route('/sales_month')
def sales_month():
    return {'data': read('data/sales_month.csv')}


@app.route('/sales_product')
def sales_product():
    with open("data/sales_product.json", 'r', encoding='utf-8') as load_f:
        load_dict = json.load(load_f)
        # print(load_dict)
    return {'data': load_dict}


@app.route('/sales_province')
def sales_province():
    return {'data': read('data/sales_province.csv')}


@app.route('/sales_region')
def sales_region():
    return {'data': read('data/sales_region.csv')}


@app.route('/sales_manager')
def sales_manager():
    return {'data': read('data/sales_manager.csv')}


if __name__ == '__main__':
    app.run()
